package com.ciwong.epaper.modules.bookstore.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.ciwong.epaper.modules.bookstore.bean.BookstoreDataBean;
import com.ciwong.epaper.modules.bookstore.ui.ItemAdvertisement;
import com.ciwong.epaper.modules.bookstore.ui.ItemGoodBookList;
import com.ciwong.epaper.modules.bookstore.ui.ItemGoodServiceList;
import com.ciwong.epaper.modules.bookstore.ui.ItemSubjectList;

/**
 * Created by Administrator on 2017/5/24.
 */
public class BookStoreAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_VIEWPAGER = 0;
    private static final int TYPE_SUBJECT_TYPE = 1;
    private static final int TYPE_GOOD_BOOK  = 2;
    private static final int TYPE_GOOD_SERVICE = 3;
//    private LoopViewPagerAdapter mPagerAdapter;
//    private List<Character> adList = new ArrayList<>();

    private BookstoreDataBean             mBookstoreDataBean;
    private RecyclerView.RecycledViewPool viewPool;

    public BookStoreAdapter() {

    }

    public void setBookstoreDataBean(BookstoreDataBean bookstoreDataBean) {
        this.mBookstoreDataBean = bookstoreDataBean;

        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int type = getItemViewType(viewType);
        switch (type) {
            case TYPE_VIEWPAGER:
                return new ViewPagerHolder(new ItemAdvertisement(parent.getContext()));
            case TYPE_SUBJECT_TYPE:
                return new SubjectEnHolder(new ItemSubjectList(parent.getContext()));
            case TYPE_GOOD_BOOK:
                return new GoodBookHoler(new ItemGoodBookList(parent.getContext()));
            case TYPE_GOOD_SERVICE:
                return new GoodsServiceHolder(new ItemGoodServiceList(parent.getContext()));

        }
        throw new IllegalArgumentException("Wrong type!");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        int type = getItemViewType(position);
        switch (type) {
            case TYPE_VIEWPAGER:
                onBindViewPagerHolder((ViewPagerHolder) viewHolder);
                break;
            case TYPE_SUBJECT_TYPE:
                onBindSubjectHolder((SubjectEnHolder) viewHolder);
                break;
            case TYPE_GOOD_BOOK:
                onGoodBookHolder((GoodBookHoler) viewHolder);
                break;
            case TYPE_GOOD_SERVICE:
                onGoodServiceHolder((GoodsServiceHolder)viewHolder);
                break;
//        }
        }
    }






    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return TYPE_VIEWPAGER;
        } else if (position==1) {
            return TYPE_SUBJECT_TYPE;
        } else if (position == 2) {
            return TYPE_GOOD_BOOK;
        } else {
            return TYPE_GOOD_SERVICE;
        }
    }

    @Override
    public int getItemCount() {
        return 4;
    }


    private void onBindViewPagerHolder(ViewPagerHolder holder) {
        if(null != mBookstoreDataBean){
            holder.mItemAdvertisement.update(mBookstoreDataBean.getBanners(),mBookstoreDataBean.getNewBook(),mBookstoreDataBean.getHotBook());
        }
    }


    private void onBindSubjectHolder(SubjectEnHolder holder) {

        if(null != mBookstoreDataBean){
            holder.mItemSubjectList.update(mBookstoreDataBean.getSubjects());
        }


//        if (mDataList.size() > 0 && position > 2 && (position - 3 < mDataList.size()))
//            ((BookStoreAdapter.AnswerHolder) holder).mItemQa.update(mDataList.get(position - 3));

    }

    private void onGoodBookHolder(GoodBookHoler holer) {
        if(null != mBookstoreDataBean){
            holer.mItemGoodBookList.update(mBookstoreDataBean.getGoodBooks());
        }
    }

    private void onGoodServiceHolder(GoodsServiceHolder holer) {
        if(null != mBookstoreDataBean){
            holer.mItemGoodServiceList.update(mBookstoreDataBean.getGoodServices());
        }
    }




    static class ViewPagerHolder extends RecyclerView.ViewHolder {

        ItemAdvertisement mItemAdvertisement;
        public ViewPagerHolder(ItemAdvertisement itemView) {
            super(itemView);
            mItemAdvertisement=itemView;

        }
    }


    static class SubjectEnHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        ItemSubjectList mItemSubjectList;
        public SubjectEnHolder(ItemSubjectList itemView) {
            super(itemView);
            mItemSubjectList=itemView;

//            itemView.findViewById(R.id.btn_door_exam).setOnClickListener(this);
//            itemView.findViewById(R.id.btn_man_bing).setOnClickListener(this);
//            itemView.findViewById(R.id.btn_shopping_mall).setOnClickListener(this);
//            itemView.findViewById(R.id.btn_traditional_therapy).setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
//            MessageEventBus messageEventBus = new MessageEventBus();
//            messageEventBus.setId(String.valueOf(v.getId()));
//            EventBus.getDefault().post(messageEventBus);
        }
    }

    static class GoodBookHoler extends RecyclerView.ViewHolder {

        ItemGoodBookList mItemGoodBookList;
        public GoodBookHoler(ItemGoodBookList itemView) {
            super(itemView);
            mItemGoodBookList=itemView;
        }
    }

    static class GoodsServiceHolder extends RecyclerView.ViewHolder {

        ItemGoodServiceList mItemGoodServiceList;
        public GoodsServiceHolder(ItemGoodServiceList itemView) {
            super(itemView);
            mItemGoodServiceList=itemView;
        }
    }
}


